/*
 *  Copyright 2011 Iurii Dunko, Iana Potochniak.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.yyminijava.syntaxtree;

import java.util.ArrayList;
import java.util.List;
import org.opu.yyminijava.visitor.Visitor;


/**
 *
 * @author Iurii Dunko
 * @author Iana  Potochniak
 */
public class ClassBody implements Rule {

    private static final long serialVersionUID = 1323110697108L;

    private List<FieldDeclaration> fields = new ArrayList<FieldDeclaration>();
    private List<MethodDeclaration> methods = new ArrayList<MethodDeclaration>();
    private ClassDeclaration clazz;

    public ClassDeclaration getClazz() {
        return clazz;
    }

    public void setClazz(ClassDeclaration clazz) {
        this.clazz = clazz;
    }

    public List<FieldDeclaration> getFields() {
        return fields;
    }
    
    public Type getFieldType(String fname){
    	for (FieldDeclaration f : fields) {
			for(String name: f.getIdentifierList().getNames()){
				if (name.equals(fname)){
					return f.getType();
				}
			}
		}
    	return null;
    }

    public void setFields(List<FieldDeclaration> fields) {
        this.fields = fields;
    }

    public List<MethodDeclaration> getMethods() {
        return methods;
    }

    public MethodDeclaration getMethod(String name, List<Type> args){
        List<MethodDeclaration> ml = new ArrayList<MethodDeclaration>();
        for (MethodDeclaration m : methods) {
            if (m.getHeader().getDeclarator().getSimpleName().equals(name)){
                ml.add(m);
            }
        }
        for (MethodDeclaration m : new ArrayList<MethodDeclaration>(ml)) {
            FormalParameterList pl = m.getHeader().getDeclarator().getParameterList();
            if ((pl != null && args.isEmpty()) || (pl == null && !args.isEmpty())){
                ml.remove(m);
                continue;
            }
            if (pl != null){
                List<FormalParameter> fps = pl.getParameters();
                if (fps.size() != args.size()){
                    ml.remove(m);
                    continue;
                }
                for (int i = 0; i < fps.size(); i++) {
                    if (!fps.get(i).getType().equals(args.get(i))){
                        return m;
                    }
                }
            }
        }
        if (ml.isEmpty()){
            return null;
        } else {
            return ml.get(0);
        }
    }

    public void setMethods(List<MethodDeclaration> methods) {
        this.methods = methods;
    }

    public void addClassMemberDeclaration(ClassMemberDeclaration member){
        member.setClazz(clazz);
        if (member instanceof FieldDeclaration){
            fields.add((FieldDeclaration) member);
        } else if (member instanceof MethodDeclaration){
            methods.add((MethodDeclaration) member);
        } else {
            // TODO: create own exception struction!
            // Using error is temporary solution, we use exception when the
            // incoming file contains errors, when the problem in our compiler
            // we use errors (but it for now)
            throw new Error("Illegal class member " + member);
        }
    }

    @Override
    public void accept(Visitor visitor) {
        for (FieldDeclaration fd : fields) {
            visitor.visit(fd);
        }

        for (MethodDeclaration fd : methods) {
            visitor.visit(fd);
        }
    }

}
